package com.example.experiment7;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    SensorManager sensorManager;
    LineChart lineChart;
    int index=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        lineChart=findViewById(R.id.lineChart);
        lineChart.setTouchEnabled(false);
        lineChart.setDescription(null);
        lineChart.getXAxis().setEnabled(true);
        lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lineChart.getAxisLeft().setEnabled(true);
        lineChart.getAxisRight().setEnabled(true);
        lineChart.getAxisLeft().setAxisMaximum(20);
        lineChart.getAxisLeft().setAxisMinimum(0);
        LineDataSet LightDataSet=new LineDataSet(new ArrayList<Entry>(),"Light");
        LightDataSet.setColors(Color.rgb(255,0,0));
        LightDataSet.setDrawCircles(false);
        LineData lineData=new LineData();
        lineData.addDataSet(LightDataSet);
        lineChart.setData(lineData);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float value[]=event.values;
        LineData lineData=lineChart.getLineData();
        LineDataSet LightDataSet=(LineDataSet)lineData.getDataSetByLabel("Light",true);
        LightDataSet.addEntry(new Entry(index++,value[0]));
        if(LightDataSet.getEntryCount()>100){
            LightDataSet.removeFirst();
            lineChart.getXAxis().setAxisMaximum(LightDataSet.getXMax());
        }
        else{
            lineChart.getXAxis().setAxisMaximum(100);
        }
        lineChart.getXAxis().setAxisMinimum(LightDataSet.getXMin());
        lineChart.getAxisLeft().setAxisMaximum(LightDataSet.getYMax());
        lineChart.notifyDataSetChanged();
        lineChart.invalidate();
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
